package com.ihomey.linkuphome.ui;

import android.app.Activity;
import android.os.Bundle;
import android.support.annotation.IdRes;
import android.util.SparseIntArray;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;

import com.github.ikidou.fragmentBackHandler.BackHandlerHelper;
import com.github.ikidou.fragmentBackHandler.FragmentBackHandler;
import com.ihomey.linkuphome.DeviceController;
import com.ihomey.linkuphome.R;
import com.ihomey.linkuphome.base.BaseFragment;
import com.ihomey.linkuphome.controller.Controller;
import com.ihomey.linkuphome.controller.ControllerFactory;
import com.ihomey.linkuphome.entities.Device;
import com.ihomey.linkuphome.entities.LightState;
import com.ihomey.linkuphome.util.BindView;
import com.ihomey.linkuphome.widget.RadioGroupPlus;

/**
 * Created by Administrator on 2017/4/20.
 */

public class SceneSettingFragment extends BaseFragment implements FragmentBackHandler, RadioGroupPlus.OnCheckedChangeListener {

    @BindView(id = R.id.radio_group_plus)
    private RadioGroupPlus radioGroupPlus;

    private DeviceController mController;
    private Controller controller;

    private SparseIntArray sparseIntArray = null;

    public static SceneSettingFragment newInstance() {
        SceneSettingFragment fragment = new SceneSettingFragment();
        return fragment;
    }

    @Override
    protected View inflaterView(LayoutInflater inflater, ViewGroup container, Bundle bundle) {
        View view = inflater.inflate(R.layout.fragment_scene_setting, null);
        return view;
    }

    @Override
    public void onAttach(Activity activity) {
        super.onAttach(activity);
        try {
            mController = (DeviceController) activity;
            controller = new ControllerFactory().createController(mController.getDeviceType());
        } catch (ClassCastException e) {
            throw new ClassCastException(activity.toString() + " must implement DeviceController callback interface.");
        }
    }

    @Override
    public void initWidget(View parentView) {
        super.initWidget(parentView);
        radioGroupPlus.setOnCheckedChangeListener(this);
    }

    @Override
    public void initData() {
        super.initData();
        sparseIntArray = new SparseIntArray();
        sparseIntArray.put(0, R.id.rb_scene_read);
        sparseIntArray.put(1, R.id.rb_scene_sunset);
        sparseIntArray.put(2, R.id.rb_scene_rest);
        sparseIntArray.put(3, R.id.rb_scene_spring);
        sparseIntArray.put(4, R.id.rb_scene_rainforest);
    }

    @Override
    public void onCheckedChanged(RadioGroupPlus group, @IdRes int checkedId) {
        if (mController.getSelectedDeviceId() != Device.DEVICE_ID_UNKNOWN) {
            controller.setLightScene(mController.getSelectedDeviceId(), sparseIntArray.indexOfValue(checkedId)+1);
        }
    }

    @Override
    public void onPause() {
        super.onPause();
        if (mController.getSelectedDeviceId() != Device.DEVICE_ID_UNKNOWN) {
            LightState lightState = mController.getLightState(mController.getSelectedDeviceId());
            if (lightState != null) {
                lightState.setSceneMode(sparseIntArray.indexOfValue(radioGroupPlus.getCheckedRadioButtonId())+1);
                mController.updateDeviceLightState(lightState);
            }
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        MainActivity mainActivity = (MainActivity) getActivity();
        mainActivity.showRightBtn(true);
        mainActivity.setTitle(R.string.sceneMode);
        if (mController.getSelectedDeviceId() != Device.DEVICE_ID_UNKNOWN) {
            LightState lightState = mController.getLightState(mController.getSelectedDeviceId());
            if (lightState != null) {
                radioGroupPlus.check(sparseIntArray.get(lightState.getSceneMode()-1));
            }
        }
    }

    @Override
    public boolean onBackPressed() {
        return BackHandlerHelper.handleBackPress(this);
    }

}
